import 'package:bruno/bruno.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:interrogation/assets/const.dart';

import '../../../../../app/routes/app_pages.dart';
import '../../../../data/api.dart';
import '../controllers/mine_page_controller.dart';

class MinePageView extends GetView<MinePageController> {
  const MinePageView({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffF1F1F1),
      appBar: BrnAppBar(
        automaticallyImplyLeading: false,
        title: "我的",
        backgroundColor: const Color(0xffF1F1F1),
        showDefaultBottom: false,
        backLeadCallback: () => Get.back(),
      ),
      body: SafeArea(
        child: Column(
          children: [
            // 头部信息卡片
            Padding(
              padding: EdgeInsets.all(16.w),
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(16.r),
                ),
                padding: EdgeInsets.all(16.w),
                child: Row(
                  children: [
                    ClipRRect(
                      borderRadius: BorderRadius.circular(32.r),
                      child: CachedNetworkImage(
                        imageUrl: controller.avatar??'',
                        imageBuilder: (context, image) => CircleAvatar(
                          foregroundImage: image,
                          radius: 32,
                        ),
                        errorWidget: (context, url, error) => Container(
                          decoration: BoxDecoration(
                            color: Colors.black.withOpacity(0.5),
                            borderRadius: BorderRadius.circular(18),
                          ),
                        ),
                        placeholder: (context, url) => Container(
                          decoration: BoxDecoration(
                            color: const Color(0xffF1F1F1),
                            borderRadius: BorderRadius.circular(18),
                          ),
                        ),
                      ),
                    ),
                    SizedBox(width: 16.w),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Text(controller.nickName??'', style: TextStyle(fontSize: 18.sp, fontWeight: FontWeight.bold)),
                              SizedBox(width: 6.w),
                              Icon(Icons.edit, size: 16.w, color: Colors.blue),
                            ],
                          ),
                          SizedBox(height: 4.h),
                          Text(controller.mobile??'', style: TextStyle(color: Colors.grey, fontSize: 14.sp)),
                          SizedBox(height: 8.h),
                          SizedBox(
                            width: 160.w,
                            height: 32.h,
                            child: ElevatedButton.icon(
                              onPressed: () {},
                              icon: Icon(Icons.favorite, size: 16.w),
                              label: Text('查看健康数据', style: TextStyle(fontSize: 14.sp)),
                              style: ElevatedButton.styleFrom(
                                backgroundColor: Colors.green[600],
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(16.r),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            // 功能区
            Expanded(
              child: ListView.separated(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                itemCount: 4,
                separatorBuilder: (_, __) => SizedBox(height: 12.h),
                itemBuilder: (context, index) {
                  final items = [
                    // {'icon': Icons.group, 'title': '家庭健康'},
                    // {'icon': Icons.person_add, 'title': '就诊人管理'},
                    // {'icon': Icons.history, 'title': '历史记录'},
                    // {'icon': Icons.security, 'title': '安全中心'},
                    // {'icon': Icons.notifications, 'title': '通知管理'},
                    // {'icon': Icons.help, 'title': '帮助中心'},
                    {'icon': Icons.dataset_linked, 'title': '隐私协议&服务政策'},
                    {'icon': Icons.add_alert_rounded, 'title': '版本号'},
                    {'icon': Icons.deck_rounded, 'title': '关于我们'},
                    {'icon': Icons.backspace_outlined, 'title': '退出登录'},
                  ];
                  return Card(
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12.r),
                    ),
                    color: Colors.white,
                    elevation: 0,
                    child: ListTile(
                      leading: Icon(items[index]['icon'] as IconData, color: Colors.green[600], size: 28.w),
                      title: Text(items[index]['title'] as String, style: TextStyle(fontSize: 16.sp)),
                      trailing: Icon(Icons.chevron_right, color: Colors.grey),
                      onTap: () {
                        if (items[index]['title'] == '关于我们') {
                          Get.toNamed(Routes.ABOUT_US);
                        }
                        if (items[index]['title'] == '隐私协议&服务政策') {
                          Get.toNamed(Routes.APP_AGREEMENTS);
                        }
                        if (items[index]['title'] == '版本号') {
                          BrnDialogManager.showConfirmDialog(context,
                              title: "当亲的版本号是版本号",
                              cancel: '取消',
                              confirm: '确定',
                              message: "1.0.0",
                              barrierDismissible: false, onConfirm: () {
                                Get.back();
                              },onCancel: () {
                            Get.back();
                          }
                          );
                        }

                        if (items[index]['title'] == '退出登录') {
                          BrnDialogManager.showConfirmDialog(context,
                              title: "确定退出登录",
                              cancel: '取消',
                              confirm: '确定',
                              message: "",
                              barrierDismissible: false, onConfirm: () {
                            Get.offAllNamed(Routes.LOGIN);
                            Api.loginOut();
                          }, onCancel: () {
                            Get.back();
                          });
                        }
                      },
                      contentPadding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.h),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
